#include "Arduino.h"
#include "BarcodeItem.h"

BarcodeItem::BarcodeItem()
{
	Reset();
}

void BarcodeItem::AddDefect(uint8_t defect_id, uint8_t qty)
{	
	EEPROM_writeAnything(defect_id + ee_defect_index, qty);
	//uint8_t old_index = FindDefectIndex(defect_id);
	//uint8_t index;
	//
	//if(old_index == -1)
	//{
		//_defectIndex++;
		//_totalDefectQty += qty;
		//index = _defectIndex;
	//}
	//else
	//index = old_index;
	//
	//defectItem[index].DEFECT_ID = defect_id;
	//defectItem[index].QTY = qty;
}

void BarcodeItem::RemoveDefect(uint8_t defect_id)
{
	if(defect_id > _max_defect_count) return;
	
	EEPROM_writeAnything(defect_id + ee_defect_index, 0);
	
	//struct DefectItem d[50];
	//int8_t n = 0;
	//
	//for(int8_t x = 0; x < _defectIndex; x++)
	//{
		//if(defectItem[x].DEFECT_ID == defect_id)
		//continue;
		//d[n] = defectItem[x];
		//n++;
	//}
	//
	//for(int x = 0; x < n; x++)
	//defectItem[x] = d[x];
}

void BarcodeItem::ClearDefect()
{
	for(int8_t x = 0; x< _max_defect_count;x++)
	EEPROM_writeAnything(x + ee_defect_index, 0);
}

void BarcodeItem::SetQcId(char a[6])
{
	for(uint8_t x = 0; x < 6; x++)
	EEPROM.write(ee_operator + x, a[x]);
}

char* BarcodeItem::GetQcId()
{
	EEPROM_readAnything(ee_operator, _operator);
	return _operator;
}

void BarcodeItem::SetBarcode(char* barcode)
{
	for(uint8_t x = 0; x < 10; x++)
	EEPROM.write(ee_barcode + x, barcode[x]);
}

char* BarcodeItem::GetBarcode()
{
	EEPROM_readAnything(ee_barcode, _barcode);
	return _barcode;
}

uint8_t BarcodeItem::GetHeType()
{
	EEPROM_readAnything(ee_type, _he_type);
	return _he_type;
}

void BarcodeItem::GetHeTypeChar(char &c)
{
	uint8_t h = GetHeType();
	if(h == 1)
	c = 'A';
	else if(h == 2)
	c = 'B';
	else
	c = '-';
}

void BarcodeItem::SetHeType(uint8_t he_type)
{
	EEPROM_writeAnything(ee_type, he_type);
}

void BarcodeItem::SetLabelNo(uint16_t lbl_No)
{
	EEPROM_writeAnything(ee_label_no, lbl_No);
}

uint16_t BarcodeItem::GetLabelNo()
{
	EEPROM_readAnything(ee_label_no, _labelNo);
	return _labelNo;
}

uint16_t BarcodeItem::GetGoodQty()
{
	EEPROM_readAnything(ee_good_qty, _goodQty);
	return _goodQty;
}

void BarcodeItem::SetGoodQty(uint16_t good_qty)
{
	EEPROM_writeAnything(ee_good_qty, good_qty);
}

uint16_t BarcodeItem::GetTotalDefectQty()
{
	int16_t s=0;	
	for(int8_t x = 0; x< _max_defect_count;)
	{
		EEPROM_readAnything(x + ee_defect_index, s);
	}
	_totalDefectQty = s;
	
	return _totalDefectQty;
}

uint16_t BarcodeItem::GetDefectQty(uint8_t defect_id)
{
	if(defect_id > _max_defect_count) return 0;
		
	EEPROM_readAnything(defect_id + ee_defect_index, _defQty);
	return _defQty;
	
	//for(int x = 0; x <= _defectIndex; x++)
	//if(defectItem[x].DEFECT_ID == defect_id)
	//return defectItem[x].QTY;
	return 0;
}

// int8_t BarcodeItem::IsReady()
// {
// 	EEPROM_readAnything(ee_ready,_is_ready);
// 	return _is_ready;
// }

//int8_t BarcodeItem::GetNumDefect()
//{
	//return _totalDefect;
//}

//int8_t BarcodeItem::FindDefectIndex(uint8_t defect_id)
//{
	//for(int x = 0; x <= _defectIndex; x++)
	//if(defectItem[x].DEFECT_ID == defect_id)
	//return x;
	//return -1;
//}

void BarcodeItem::Reset()
{
	//for(int8_t x=0;x<sizeof(_barcode);x++)
	//_barcode[x]=NULL;
	//for(int8_t x=0;x<sizeof(_operator);x++)
	//_operator[x]=NULL;
	//
	//_he_type = -1;
	//_labelNo = 0;
	//_totalDefect = 0;
	//_defectIndex = -1;
	//_orderQty = 0;
	//_goodQty = 0;
	//_defQty = 0;
	ClearDefect();
}